﻿<View
    x:Name="This"
    x:Class="Poet.Views.SearchView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:converters="clr-namespace:Poet.Converters"
    xmlns:regularExpressions="clr-namespace:System.Text.RegularExpressions;assembly=System"
    VerticalAlignment="Stretch"
    MouseDown="SearchView_OnMouseDown"
    MouseWheel="ScrollMatch">

    <View.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Poet;component/Resources/XamlStore.xaml"/>
            </ResourceDictionary.MergedDictionaries>
            <converters:MoveButtonCursorConverter x:Key="MoveButtonCursorConverter"/>
            <converters:ItemsSelectorTextConverter x:Key="ItemsSelectorTextConverter"/>
        </ResourceDictionary>
    </View.Resources>
    
    <View.ContextMenu>
        <ContextMenu Name="ViewContextMenu">
            <MenuItem 
                Header="{Localizing Options}" 
                Style="{StaticResource CheckableStyle}"
                IsChecked="{ViewModel ShowOptions, DefaultValue=False}"/>
            <Separator/>
            <MenuItem 
                Header="{Localizing Common}" 
                IsChecked="{ViewModel ShowCommonOptions, DefaultValue=True}"
                Style="{StaticResource CheckableStyle}"
                ItemsSource="{VisibilityMenuItems SourceName=CommonOptions, StyleStaticKey=CheckableItemStyle}"/>
            <MenuItem 
                Header="{Localizing Separator}" 
                IsChecked="{ViewModel ShowSeparator, DefaultValue=True}"
                Style="{StaticResource CheckableStyle}"/>
            <MenuItem 
                Header="{Localizing Regex}" 
                IsChecked="{ViewModel ShowRegexOptions, DefaultValue=True}" 
                Style="{StaticResource CheckableStyle}"
                ItemsSource="{VisibilityMenuItems SourceName=RegexOptions, StyleStaticKey=CheckableItemStyle}"/>
        </ContextMenu>
    </View.ContextMenu>

    <StackPanel Margin="3">
        <StackPanel.Resources>
            <Style TargetType="TextBlock">
                <Setter Property="Margin" Value="3"/>
            </Style>
        </StackPanel.Resources>

        <TextBlock Text="{Localizing Pattern, Tail={Localizing Text, StringFormat='{} ({0})', Transformer={x:Static LocalizingManager.DefaultWithToLower}}}" Visibility="{Binding UseRegex, Converter={StaticResource BooleanToVisibilityInvertedConverter}}"/>
        <ComboBox IsTextSearchCaseSensitive="True" IsEditable="True"  Name="TextPatternComboBox" Text="{Binding TextPattern, Mode=TwoWay}" ItemsSource="{Binding RecentTextPatterns}" Visibility="{Binding UseRegex, Converter={StaticResource BooleanToVisibilityInvertedConverter}}"/>
        <TextBlock Text="{Localizing Pattern, Tail={Localizing Regex, StringFormat='{} ({0})', Transformer={x:Static LocalizingManager.DefaultWithToLower}}}" Visibility="{Binding UseRegex, Converter={StaticResource BooleanToVisibilityConverter}}"/>
        <ComboBox IsTextSearchCaseSensitive="True" IsEditable="True" Name="RegexPatternComboBox" Text="{Binding RegexPattern, ValidatesOnDataErrors=True, Mode=TwoWay}" ItemsSource="{Binding RecentRegexPatterns}" Visibility="{Binding UseRegex, Converter={StaticResource BooleanToVisibilityConverter}}"/>

        <TextBlock Text="{Localizing Order}"/>
        <ComboBox
            IsEditable="True"
            Name="SearchItemsComboBox"
            DataContext="{Binding LookInViewModel}"
            ItemsSource="{Binding Items}"
            SelectionChanged="IgnoreSelectionChanged"
            Text="{Binding ActiveItem.DataContext.Title, Mode=OneWay, FallbackValue={Localizing None}}"
            Template="{StaticResource SearchComboBoxTemplate}"
            ItemContainerStyle="{StaticResource SearchComboBoxItemStyle}">

            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <SelectableItem
                        Padding="3,2"
                        x:Name="SelectableItem"
                        Background="Transparent"    
                        MouseMove="SelectableItem_OnPreviewMouseMove"
                        PreviewMouseDown="SelectableItem_OnMouseDown" 
                        Cursor="{Binding ElementName=SearchItemsComboBox, Path=DataContext.ItemForMove, Converter={StaticResource MoveButtonCursorConverter}}"
                        SelectedItems="{Binding DataContext.SelectedItems, ElementName=SearchItemsComboBox}">

                        <Grid Width="Auto">

                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="16"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>

                            <CheckBox 
                                    Grid.Column="0" 
                                    Name="CheckBox"
                                    IsHitTestVisible="False"
                                    IsChecked="{Binding IsSelected, Mode=TwoWay, ElementName=SelectableItem}" 
                                    IsEnabled="{Binding IsChecked, Mode=OneWay, ElementName=RadioButton, Converter={StaticResource BooleanInvertConverter}}">
                                <TextBlock Text="{Binding Title}"/>
                            </CheckBox>

                            <Rectangle
                                    Grid.Column="1"
                                    Name="MoveButton"
                                    Margin="2"
                                    RadiusX="2"   
                                    RadiusY="2"   
                                    Fill="Beige"
                                    Stroke="Silver"
                                    StrokeThickness="1"
                                    PreviewMouseDown="SelectableItem_OnMouseDown"
                                    ToolTipService.InitialShowDelay="4000"
                                    ToolTipService.BetweenShowDelay="4000"
                                    ToolTip="{Localizing MoveDocument}">
                                <!--<Rectangle.Triggers>
                                        <EventTrigger RoutedEvent="Rectangle.MouseEnter">
                                            <EventTrigger.Actions>
                                                <BeginStoryboard>
                                                    <Storyboard>
                                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill">
                                                            <DiscreteObjectKeyFrame KeyTime="0:0:0">
                                                                <DiscreteObjectKeyFrame.Value>
                                                                    <SolidColorBrush Color="Red"/>
                                                                </DiscreteObjectKeyFrame.Value>
                                                            </DiscreteObjectKeyFrame>
                                                        </ObjectAnimationUsingKeyFrames>
                                                    </Storyboard>
                                                </BeginStoryboard>
                                            </EventTrigger.Actions>
                                        </EventTrigger>
                                    </Rectangle.Triggers>-->
                            </Rectangle>

                            <RadioButton 
                                    Grid.Column="2"
                                    Name="RadioButton"
                                    VerticalAlignment="Center"
                                    HorizontalAlignment="Center" 
                                    PreviewMouseDown="SetActiveItem" 
                                    IsChecked="{Binding IsActiveDocument, Mode=OneWay}"
                                    ToolTipService.InitialShowDelay="4000"
                                    ToolTipService.BetweenShowDelay="4000"
                                    ToolTip="{Localizing SetActiveDocument}"/>

                        </Grid>

                    </SelectableItem>

                    <DataTemplate.Triggers>

                        <DataTrigger Binding="{Binding IsActiveDocument}" Value="True">
                            <Setter TargetName="CheckBox" Property="Foreground" Value="Red"/>
                        </DataTrigger>

                        <DataTrigger Binding="{Binding IsActiveDocument}" Value="True">
                            <Setter TargetName="CheckBox" Property="IsChecked" Value="True"/>
                        </DataTrigger>

                    </DataTemplate.Triggers>
                </DataTemplate>
            </ComboBox.ItemTemplate>

        </ComboBox>

        <StackPanel>
            <StackPanel.Resources>
                <Style TargetType="Button">
                    <Setter Property="Padding" Value="8 1 8 1"/>
                    <Setter Property="Margin" Value="8 0 0 0"/>
                </Style>
                <Style TargetType="Expander">
                    <Setter Property="Margin" Value="0 8 0 8"/>
                </Style>
            </StackPanel.Resources>

            <Expander IsExpanded="{ViewModel IsSeekExpanded, DefaultValue=True}">
                <Expander.Header>
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" MouseDown="Handle">
                        <StackPanel.Resources>
                            <Style TargetType="{x:Type ToggleButton}" BasedOn="{StaticResource {x:Type ToggleButton}}">
                                <Setter Property="Padding" Value="8 1 8 1"/>
                                <Setter Property="Margin" Value="8 0 0 0"/>
                                <Setter Property="VerticalContentAlignment" Value="Bottom"/>
                                <Setter Property="HorizontalContentAlignment" Value="Left"/>
                            </Style>
                        </StackPanel.Resources>
                        <!--<TextBlock Text="{Localizing Seek, StringFormat='\{0}:'}" VerticalAlignment="Center"/>-->
                        <Button Content="{Picture Prev}" Command="ApplicationCommands.Find" CommandParameter="Back" ToolTip="{Localizing SeekBack}"/>
                        <Button Content="{Picture Next}" Command="ApplicationCommands.Find" CommandParameter="Next" ToolTip="{Localizing SeekNext}"/>
                        <ToggleButton
                            ToolTip="{Localizing Options}"
                            IsChecked="{Binding IsOpen, Mode=TwoWay, ElementName=ContextMenu}"
                            VerticalAlignment="Center"  
                            Content="{Picture Settings}">
                            <ToggleButton.ContextMenu>
                                <ContextMenu Name="ContextMenu">
                                    <ContextMenu.Resources>
                                        <Style TargetType="{x:Type MenuItem}">
                                            <Setter Property="IsCheckable" Value="True"/>
                                            <Setter Property="StaysOpenOnClick" Value="True"/>
                                        </Style>
                                    </ContextMenu.Resources>
                                    <MenuItem
                                        Header="{Localizing IgnoreCase}"    
                                        IsChecked="{Binding RegexOptions, 
                                        ConverterParameter={x:Static regularExpressions:RegexOptions.IgnoreCase},
                                        Converter={StaticResource FlagConverter}}"/>
                                    <MenuItem 
                                        Header="{Localizing WholeWord}" 
                                        IsChecked="{Binding WholeWord}"/>
                                    <MenuItem 
                                        Header="{Localizing RightToLeft}" 
                                        IsChecked="{Binding RegexOptions, 
                                        ConverterParameter={x:Static regularExpressions:RegexOptions.RightToLeft}, 
                                        Converter={StaticResource FlagConverter}}"/>
                                    <Separator/>
                                    <MenuItem Header="{Localizing Regex}" IsChecked="{Binding UseRegex}"/>
                                    <Separator Visibility="{Binding UseRegex, Converter={StaticResource BooleanToVisibilityConverter}}"/>
                                    <MenuItem 
                                        Header="{Localizing Singleline}" 
                                        Visibility="{Binding UseRegex, Converter={StaticResource BooleanToVisibilityConverter}}"
                                        IsChecked="{Binding RegexOptions, 
                                        ConverterParameter={x:Static regularExpressions:RegexOptions.Singleline},
                                        Converter={StaticResource FlagConverter}}"/>
                                    <MenuItem 
                                        Header="{Localizing Multiline}"
                                        Visibility="{Binding UseRegex, Converter={StaticResource BooleanToVisibilityConverter}}"
                                        IsChecked="{Binding RegexOptions, 
                                        ConverterParameter={x:Static regularExpressions:RegexOptions.Multiline},
                                        Converter={StaticResource FlagConverter}}"/>
                                    <MenuItem 
                                        Header="{Localizing ExplicitCapture}"
                                        Visibility="{Binding UseRegex, Converter={StaticResource BooleanToVisibilityConverter}}"
                                        IsChecked="{Binding RegexOptions, 
                                        ConverterParameter={x:Static regularExpressions:RegexOptions.ExplicitCapture},
                                        Converter={StaticResource FlagConverter}}"/>
                                    <MenuItem 
                                        Header="{Localizing CultureInvariant}" 
                                        Visibility="{Binding UseRegex, Converter={StaticResource BooleanToVisibilityConverter}}"
                                        IsChecked="{Binding RegexOptions, 
                                        ConverterParameter={x:Static regularExpressions:RegexOptions.CultureInvariant},
                                        Converter={StaticResource FlagConverter}}"/>
                                    <MenuItem 
                                        Header="{Localizing IgnorePatternWhitespace}" 
                                        Visibility="{Binding UseRegex, Converter={StaticResource BooleanToVisibilityConverter}}"
                                        IsChecked="{Binding RegexOptions, 
                                        ConverterParameter={x:Static regularExpressions:RegexOptions.IgnorePatternWhitespace},
                                        Converter={StaticResource FlagConverter}}"/>
                                    <MenuItem 
                                        Header="{Localizing ECMAScript}" 
                                        Visibility="{Binding UseRegex, Converter={StaticResource BooleanToVisibilityConverter}}"
                                        IsChecked="{Binding RegexOptions, 
                                        ConverterParameter={x:Static regularExpressions:RegexOptions.ECMAScript},
                                        Converter={StaticResource FlagConverter}}"/>
                                </ContextMenu>
                            </ToggleButton.ContextMenu>
                        </ToggleButton>
                    </StackPanel>
                </Expander.Header>

                <StackPanel>
                    <StackPanel.Resources>

                        <TextBlock Text="{Localizing NoMatches}" x:Key="NoMatches" x:Shared="False"/>

                        <SwitchConverter x:Key="CountConverter" x:Shared="False">
                            <Case Key="0">
                                <TextBlock Text="{Localizing NoMatches}"/>
                            </Case>
                            <SwitchConverter.Default>
                                <TextBlock Text="{Binding ActiveDocumentView.DataContext.Matches.Count}"/>
                            </SwitchConverter.Default>
                        </SwitchConverter>

                    </StackPanel.Resources>

                    <ScrollBar
                        Name="ScrollBar"
                        Focusable="True"
                        Margin="0 6 0 0"
                        Orientation="Horizontal"
                        HorizontalAlignment="Stretch"
                        Value="{Binding ActiveDocumentView.DataContext.NearestMatchIndex, TargetNullValue='-1'}" 
                        SmallChange="1"
                        LargeChange="1"
                        ViewportSize="5"
                        Minimum="0"
                        Maximum="{Binding ActiveDocumentView.DataContext.Matches.Count, Converter={StaticResource DecementConverter}, FallbackValue=0, TargetNullValue=0}"/>

                    <ContentControl 
                        HorizontalAlignment="Center" IsHitTestVisible="False">
                        <ContentControl.Resources>
                            <Style TargetType="TextBlock">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding ActiveDocumentView.DataContext.IsMatchesActual, FallbackValue=False}" Value="False">
                                        <Setter Property="Foreground" Value="Gray"/>
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding ActiveDocumentView.DataContext.IsMatchesActual, FallbackValue=False}" Value="True">
                                        <Setter Property="Foreground" Value="Black"/>
                                    </DataTrigger>
                                </Style.Triggers>
                                <Setter Property="Margin" Value="2"/>
                            </Style>
                            <SwitchConverter x:Key="CollapseConverter" Default="Collapsed"/>
                        </ContentControl.Resources>

                        <StackPanel>
                            <TextBlock Text="{Localizing NotAvailable}" Visibility="{Binding ActiveDocumentView.DataContext.NearestMatchIndex, Converter={StaticResource CollapseConverter}, FallbackValue=Visible}"/>
                            <StackPanel Orientation="Horizontal" Visibility="{Binding ActiveDocumentView.DataContext.MatchIndex, Converter={StaticResource NullToVisibilityConverter}, FallbackValue=Collapsed}">
                                <TextBlock Text="{Binding ActiveDocumentView.DataContext.MatchIndex, Converter={StaticResource IncrementConverter}, FallbackValue={x:Null}, TargetNullValue={x:Null}}"/>
                                <TextBlock Text="{Binding ActiveDocumentView.DataContext.MatchIndex, StringFormat={Localizing Of}, FallbackValue={x:Null}, TargetNullValue={x:Null}}"/>
                                <ContentControl Content="{Binding ActiveDocumentView.DataContext.Matches.Count, Converter={StaticResource CountConverter}, FallbackValue={StaticResource NoMatches}}"/>
                            </StackPanel>
                            <StackPanel Orientation="Horizontal" Visibility="{Binding ActiveDocumentView.DataContext.MatchIndex, Converter={StaticResource NullToVisibilityInvertedConverter}, FallbackValue=Collapsed}">
                                <TextBlock Text="{Binding ActiveDocumentView.DataContext.NearestMatchIndex, StringFormat='~{0}', Converter={StaticResource IncrementConverter}, FallbackValue={x:Null}, TargetNullValue={x:Null}}"/>
                                <TextBlock Text="{Binding ActiveDocumentView.DataContext.NearestMatchIndex, StringFormat={Localizing Of}, FallbackValue={x:Null}, TargetNullValue={x:Null}}"/>
                                <ContentControl Content="{Binding ActiveDocumentView.DataContext.Matches.Count, Converter={StaticResource CountConverter}, FallbackValue={StaticResource NoMatches}}"/>
                            </StackPanel>
                        </StackPanel>
                    </ContentControl>

                </StackPanel>

            </Expander>

            <StackPanel Visibility="{Binding IsReplaceMode, ElementName=This, Converter={StaticResource BooleanToVisibilityConverter}}">
                <Separator Margin="0 0 0 2"/>
                <TextBlock Text="{Localizing Template}"/>
                <ComboBox 
                        IsEditable="True"
                        Name="ReplacePatternComboBox" 
                        Text="{Binding ReplaceTemplate, ValidatesOnDataErrors=True, Mode=TwoWay}"
                        ItemsSource="{Binding RecentReplaceTemplates}"/>
                <Expander IsExpanded="{ViewModel IsReplaceExpanded, DefaultValue=False}">
                    <Expander.Header>
                        <StackPanel MouseDown="Handle">
                            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Visibility="{Binding IsChecked, ElementName=SeekWithReplaceRadioButton, Converter={StaticResource BooleanToVisibilityConverter}}">
                                <Button Content="{Picture ReplaceBack}" Command="ApplicationCommands.Replace" CommandParameter="Back" ToolTip="{Localizing ReplaceAndSeekBack}"/>
                                <Button Content="{Picture ReplaceNext}" Command="ApplicationCommands.Replace" CommandParameter="Next" ToolTip="{Localizing ReplaceAndSeekNext}"/>
                                <Button Content="{Picture Replace}" Command="ApplicationCommands.Replace" CommandParameter="Single" ToolTip="{Localizing Replace}"/>
                            </StackPanel>

                            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Visibility="{Binding IsChecked, ElementName=ReplaceAllRadioButton, Converter={StaticResource BooleanToVisibilityConverter}}">
                                <Button Content="{Picture ReplaceAllBack}" Command="ApplicationCommands.Replace" CommandParameter="All Back" ToolTip="{Localizing ReplaceAllBack}"/>
                                <Button Content="{Picture ReplaceAllNext}" Command="ApplicationCommands.Replace" CommandParameter="All Next" ToolTip="{Localizing ReplaceAllNext}"/>
                                <Button Content="{Picture ReplaceAll}" Command="ApplicationCommands.Replace" CommandParameter="All" ToolTip="{Localizing ReplaceAll}"/>
                            </StackPanel>
                        </StackPanel>
                    </Expander.Header>

                    <StackPanel>
                        <RadioButton Margin="5 4 0 0" Content="{Localizing SeekWithReplace, StringFormat='\{0}:'}" Name="SeekWithReplaceRadioButton" IsChecked="True"/>
                        <RadioButton Margin="5 4 0 0" Content="{Localizing ReplaceAll, StringFormat='\{0}:'}" Name="ReplaceAllRadioButton"/>
                    </StackPanel>
                </Expander>
            </StackPanel>

            <GroupBox Header="{Localizing Options}" Visibility="{ViewModel ShowOptions, DefaultValue=False}">
                <StackPanel>
                    <StackPanel.Resources>
                        <Style TargetType="TextBlock">
                            <Setter Property="Margin" Value="0"/>
                        </Style>
                        <Style TargetType="CheckBox">
                            <Setter Property="Margin" Value="2"/>
                        </Style>
                    </StackPanel.Resources>
                    <StackPanel Name="CommonOptions" Visibility="{ViewModel ShowCommonOptions, DefaultValue=True}">
                        <CheckBox     
                            Tag="IgnoreCase"
                            Content="{Localizing IgnoreCase}"    
                            Visibility="{ViewModel DefaultValue=True}"
                            IsChecked="{Binding RegexOptions, 
                            ConverterParameter={x:Static regularExpressions:RegexOptions.IgnoreCase},
                            Converter={StaticResource FlagConverter}}"/>
                        <CheckBox        
                            Tag="WholeWord"
                            Content="{Localizing WholeWord}"   
                            Visibility="{ViewModel DefaultValue=True}"
                            IsChecked="{Binding WholeWord}"/>
                        <CheckBox 
                            Tag="RightToLeft"
                            Content="{Localizing RightToLeft}" 
                            Visibility="{ViewModel DefaultValue=True}"
                            IsChecked="{Binding RegexOptions, 
                            ConverterParameter={x:Static regularExpressions:RegexOptions.RightToLeft}, 
                            Converter={StaticResource FlagConverter}}"/>
                    </StackPanel>
                    <Separator Visibility="{ViewModel ShowSeparator, DefaultValue=True}"/>
                    <StackPanel Name="RegexOptions" Visibility="{ViewModel ShowRegexOptions, DefaultValue=True}">
                        <CheckBox 
                            Tag="Singleline"
                            Content="{Localizing Singleline}" 
                            Visibility="{ViewModel DefaultValue=Visible}"
                            IsChecked="{Binding RegexOptions, 
                            ConverterParameter={x:Static regularExpressions:RegexOptions.Singleline},
                            Converter={StaticResource FlagConverter}}"/>
                        <CheckBox 
                            Tag="Multiline"
                            Content="Multiline"
                            Visibility="{ViewModel DefaultValue=True}"
                            IsChecked="{Binding RegexOptions, 
                            ConverterParameter={x:Static regularExpressions:RegexOptions.Multiline},
                            Converter={StaticResource FlagConverter}}"/>
                        <CheckBox         
                            Tag="ExplicitCapture"
                            Content="{Localizing ExplicitCapture}"
                            Visibility="{ViewModel DefaultValue=True}"
                            IsChecked="{Binding RegexOptions, 
                            ConverterParameter={x:Static regularExpressions:RegexOptions.ExplicitCapture},
                            Converter={StaticResource FlagConverter}}"/>
                        <CheckBox     
                            Tag="CultureInvariant"
                            Content="{Localizing CultureInvariant}" 
                            Visibility="{ViewModel DefaultValue=True}"
                            IsChecked="{Binding RegexOptions, 
                            ConverterParameter={x:Static regularExpressions:RegexOptions.CultureInvariant},
                            Converter={StaticResource FlagConverter}}"/>
                        <CheckBox     
                            Tag="IgnorePatternWhitespace"
                            Content="{Localizing IgnorePatternWhitespace}" 
                            Visibility="{ViewModel DefaultValue=True}"
                            IsChecked="{Binding RegexOptions, 
                            ConverterParameter={x:Static regularExpressions:RegexOptions.IgnorePatternWhitespace},
                            Converter={StaticResource FlagConverter}}"/>
                        <CheckBox     
                            Tag="ECMAScript"
                            Content="{Localizing ECMAScript}" 
                            Visibility="{ViewModel DefaultValue=True}"
                            IsChecked="{Binding RegexOptions, 
                            ConverterParameter={x:Static regularExpressions:RegexOptions.ECMAScript},
                            Converter={StaticResource FlagConverter}}"/>
                    </StackPanel>
                </StackPanel>
            </GroupBox>

        </StackPanel>
    </StackPanel>

</View>